package ru.compft.testing.utils;

import org.springframework.util.StringUtils;
import ru.compft.common.parser.XmlFileParser;
import ru.compft.common.utils.GlobalConstants;
import ru.compft.testing.datamodel.*;

import java.util.Date;
import java.util.List;

/**
 * User: VVasiliev
 * Date: 24.08.2011
 * Time: 14:58:18
 * <p/>
 * Построитель тестов
 */
public class JavaTestBuilder {

    private static RootJAXBElement jaxbElement = new RootJAXBElement();
    private static final String path = "/testData/java";

    private String topic;
    private String level;
    private String question;
    private String answer;

    public void setAnswer(String answer) {
        this.answer = answer;
    }

    public JavaTestBuilder(String topic, String level, String question) {
        this.topic = topic;
        this.level = level;
        this.question = question;
    }

    public boolean buildTest() {
        try {
            jaxbElement.getJavaQuiz().add(createTest());
        } catch (Exception e) {
            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
        }
        return true;
    }

    /**
     * Сколько всего тестов в корневом элементе
     *
     * @return
     */
    public static int countAllTests() {
        return jaxbElement.getJavaQuiz().size();
    }

    public static List getAllTests() {
        return jaxbElement.getJavaQuiz();
    }

    /**
     * Записать тесты в файл
     */
    public static void writeTests() {
        final String filePath = createFilePath();
        final String fileName = createFileName();
        System.out.println("Write tests to " + fileName);
        XmlFileParser.writeJAXBElementToXml(jaxbElement, RootJAXBElement.class, filePath, fileName);
    }

    /**
     * Прочитать тесты из файла
     */
    public static void readTests() {
        final String filePath = createFilePath();
        final String fileName = createFileName();
        System.out.println("Write tests to " + fileName);
        final Object result = XmlFileParser.readJAXBElementFromXml(RootJAXBElement.class, filePath, fileName);
        if (result != null) jaxbElement = (RootJAXBElement) result;
    }

    private static String createFilePath() {
        final StringBuilder builder = new StringBuilder();
        builder.append(GlobalConstants.USER_DIR)
                .append(GlobalConstants.SLASH)
                .append(path);
        return builder.toString();
    }

    private static String createFileName() {
        final StringBuilder builder = new StringBuilder();
        builder.append(GlobalConstants.SIMPLE_FORMAT.format(new Date()))
                .append(GlobalConstants.XML_FILE_SUFFIX);
        return builder.toString();
    }

    private JavaQuiz createTest() throws Exception {
        final Topic createdTopic = createTopic(topic);
        if (createdTopic == null) throw new Exception("createdTopic from [" + topic + "] is null");

        final DifficultyLevel createdLevel = createLevel(level);
        if (createdLevel == null) throw new Exception("createdLevel from [" + level + "] is null");

        final Question createdQuestion = createQuestion(question);
        if (createdQuestion == null) throw new Exception("createdQuestion from [" + question + "] is null");

        final JavaQuiz javaTest = new JavaQuiz();
        javaTest.setTopic(createdTopic);
        javaTest.setDifficultyLevel(createdLevel);
        javaTest.setQuestion(createdQuestion);

        return javaTest;
    }

    private Question createQuestion(String value) throws Exception {
        if (StringUtils.hasText(value)) {
            final Question question = new Question();
            question.setValue(value);

            return question;
        }
        return null;
    }

    private DifficultyLevel createLevel(String code) throws Exception {
        return StringUtils.hasText(code) ? DifficultyLevel.valueOf(code) : null;
    }

    private Topic createTopic(String name) throws Exception {
        if (StringUtils.hasText(name)) {
            final Topic topic = new Topic();
            topic.setHeader(name);

            return topic;
        }
        return null;
    }
}
